﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace LdCms.BLL.Sms
{
    using LdCms.EF.DbEntitiesContext;
    using LdCms.EF.DbModels;
    using LdCms.EF.DbStoredProcedure;
    using LdCms.IBLL.Sms;
    using LdCms.IDAL.Sms;
    using LdCms.Common.Json;
    using LdCms.Common.Security;
    using LdCms.Common.Extension;
    using LdCms.Common.Utility;
    using System.Linq.Expressions;

    public partial class FanWeSendRecordService:BaseService<Ld_Sms_FanWeSendRecord>,IFanWeSendRecordService
    {
        private readonly IFanWeSendRecordDAL FanWeSendRecordDAL;
        private readonly LdCmsDbEntitiesContext LdCmsDbEntitiesContext;
        public FanWeSendRecordService(LdCmsDbEntitiesContext LdCmsDbEntitiesContext, IFanWeSendRecordDAL FanWeSendRecordDAL)
        {
            this.LdCmsDbEntitiesContext = LdCmsDbEntitiesContext;
            this.FanWeSendRecordDAL = FanWeSendRecordDAL;
            this.Dal = FanWeSendRecordDAL;
        }
        public override void SetDal()
        {
            Dal = FanWeSendRecordDAL;
        }

        public bool SaveFanWeSendRecord(Ld_Sms_FanWeSendRecord entity)
        {
            try
            {
                int systemId = entity.SystemID;
                string companyId = entity.CompanyID;
                string platformId = entity.PlatformID;
                string sendId = entity.SendID;
                if (string.IsNullOrEmpty(sendId))
                {
                    var primarykey = PrimaryKeyHelper.PrimaryKeyType.SmsFanWeSendRecord;
                    var primaryKeyLen = PrimaryKeyHelper.PrimaryKeyLen.V2;
                    entity.SendID = PrimaryKeyHelper.MakePrimaryKey(primarykey, primaryKeyLen);
                }
                entity.CreateDate = DateTime.Now;
                return Add(entity);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public bool DeleteFanWeSendRecord(int systemId, string companyId, string platformId, string sendId)
        {
            try
            {
                return Delete(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId && m.SendID == sendId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public Ld_Sms_FanWeSendRecord GetFanWeSendRecord(int systemId, string companyId, string platformId, string sendId)
        {
            try
            {
                return Find(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId && m.SendID == sendId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_FanWeSendRecord> GetFanWeSendRecordTop(int systemId, string companyId, int count)
        {
            try
            {
                int total = Utility.ToTopTotal(count);
                var expression = ExtLinq.True<Ld_Sms_FanWeSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                var expressionScalarLambda = GetExpressionScalarLambda();
                return FindListTop(expression, expressionScalarLambda, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_FanWeSendRecord> GetFanWeSendRecordPaging(int systemId, string companyId, int pageId, int pageSize)
        {
            try
            {
                var expression = ExtLinq.True<Ld_Sms_FanWeSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId);
                var expressionScalarLambda = GetExpressionScalarLambda();
                int pageIndex = Utility.ToPageIndex(pageId);
                int pageCount = Utility.ToPageCount(pageSize);
                return FindListPaging(expression, expressionScalarLambda, m => m.CreateDate, false, pageIndex, pageCount).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public List<Ld_Sms_FanWeSendRecord> SearchFanWeSendRecord(int systemId, string companyId, string startTime, string endTime, string state, string keyword, int count)
        {
            try
            {
                DateTime dateStartTime = Utility.ToStartTime(startTime);
                DateTime dateEndTime = Utility.ToEndTime(endTime);
                bool isResult = state.ToBool();
                int total = Utility.ToTopTotal(count);
                //条件
                var expression = ExtLinq.True<Ld_Sms_FanWeSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId
                && m.CreateDate.Value.Date >= dateStartTime.Date && m.CreateDate.Value.Date <= dateEndTime.Date
                && (string.IsNullOrEmpty(state) ? true : m.IsResult.Value == isResult)
                && m.Mobile.Contains(keyword));
                //字段
                var expressionScalarLambda = GetExpressionScalarLambda();
                //执行
                return FindListTop(expression, expressionScalarLambda, m => m.CreateDate, false, total).ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public int CountFanWeSendRecord(int systemId, string companyId)
        {
            try
            {
                return Count(m => m.SystemID == systemId && m.CompanyID == companyId);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public int CountFanWeSendRecord(int systemId, string companyId, string startTime, string endTime, string state, string keyword)
        {
            try
            {
                DateTime dateStartTime = Utility.ToStartTime(startTime);
                DateTime dateEndTime = Utility.ToEndTime(endTime);
                bool isResult = state.ToBool();
                //条件
                var expression = ExtLinq.True<Ld_Sms_FanWeSendRecord>();
                expression = expression.And(m => m.SystemID == systemId && m.CompanyID == companyId
                && m.CreateDate.Value.Date >= dateStartTime.Date && m.CreateDate.Value.Date <= dateEndTime.Date
                && (string.IsNullOrEmpty(state) ? true : m.IsResult.Value == isResult)
                && m.Mobile.Contains(keyword));
                return Count(expression);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public bool VerifyFanWeSendCode(int systemId, string companyId, string platformId, string sendId, string phone, string code)
        {
            try
            {
                return IsExists(m => m.SystemID == systemId && m.CompanyID == companyId && m.PlatformID == platformId && m.SendID == sendId && m.Mobile == phone && m.Code == code);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        private Expression<Func<Ld_Sms_FanWeSendRecord, Ld_Sms_FanWeSendRecord>> GetExpressionScalarLambda()
        {
            try
            {
                Expression<Func<Ld_Sms_FanWeSendRecord, Ld_Sms_FanWeSendRecord>> scalarLambda = m => new Ld_Sms_FanWeSendRecord
                {
                    SystemID = m.SystemID,
                    CompanyID = m.CompanyID,
                    PlatformID = m.PlatformID,
                    SendID = m.SendID,
                    Mobile = m.Mobile,
                    Content = m.Content,
                    ReturnStatus = m.ReturnStatus,
                    Message = m.Message,
                    RemainPoint = m.RemainPoint,
                    TaskID = m.TaskID,
                    SuccessCounts = m.SuccessCounts,
                    IsResult = m.IsResult,
                    CreateDate = m.CreateDate
                };
                return scalarLambda;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

    }
}
